5bda9a
@@ -113,9 +113,30 @@
public final class ServerConfigService implements Service<ServerConfig> {
     see https://issues.apache.org/jira/browse/HARMONY-3789 & http://bouncycastle.org/jira/browse/BJA-19 for more
     */
     private static void fixSecurityProvider(){
+        reorderProviders();
+    }
+
+    /*
+    We reorder providers so BC provider comes after JCE ones from the jvm, if we cannot find index of JCE provider we put it to last place in list
+     */
+    private static void reorderProviders() {
         Provider bc = Security.getProvider("BC");
-        if (bc!=null) {
-            bc.remove("KeyPairGenerator.DH");
+        if (bc != null) { //reorder needed
+            int index = -1;
+            Provider[] providers = Security.getProviders();
+            for (int i = 0; i < providers.length; i++) {
+                Provider provider = providers[i];
+                if (provider.getName().equals("SunJCE") || provider.getName().equals("IBMJCE")) {
+                    index = i;
+                    break;
+                }
+            }
+            Security.removeProvider(bc.getName());
+            if (index > -1) {
+                Security.insertProviderAt(bc, index + 1);
+            } else {
+                Security.insertProviderAt(bc, providers.length); //add it to last place
+            }
         }
     }
 }
